package com.spica.spicaschool.ui.setting

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.widget.LinearLayout
import coil.load
import com.spica.spicaschool.R
import com.spica.spicaschool.databinding.ViewFuntionItemBinding

class FunctionItem : LinearLayout {

    constructor(context: Context) : super(context)

  constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        initView(attrs)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
        initView(attrs)
    }

    private var viewBinding: ViewFuntionItemBinding

    init {
        inflate(context, R.layout.view_funtion_item, this)
        viewBinding = ViewFuntionItemBinding.bind(this)
    }

    private fun initView(attrs: AttributeSet) {
        val array: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.FunctionItem)
        viewBinding.tvTitle.text = array.getText(R.styleable.FunctionItem_title)
        viewBinding.tvSubtitle.text = array.getText(R.styleable.FunctionItem_subtitle)
        viewBinding.image.background = array.getDrawable(R.styleable.FunctionItem_image_bg)
        viewBinding.image.load(array.getDrawable(R.styleable.FunctionItem_src))
        array.recycle()
    }
}
